﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JinfuyuanProject.Entity;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using JinfuyuanProject.Repository.Interface;
using System.Collections;
using JinfuyuanProject.Entity.Filter;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Office2010.Excel;
using JinfuyuanProject.Entity.ViewModels;
using System.Data.Entity;
using System.Data.SqlClient;
using JinfuyuanProject.Repository;

namespace JinfuyuanProject.Service
{
    public class EntryService : IEntryService
    {
        private readonly IEntryRepository _entryRepository;

        public EntryService(IEntryRepository entryRepository)
        {
            _entryRepository = entryRepository;
        }
        #region  基本方法
        public bool Delete(Entry entry)
        {
            return _entryRepository.Delete(entry);
        }

        public IQueryable<Entry> GetList()
        {
            return _entryRepository.GetList();
        }

        public Entry GetModel(int Id)
        {
            return _entryRepository.GetModel(Id);
        }

        public int Insert(Entry entry)
        {
            return _entryRepository.Insert(entry);
        }

        public bool Update(Entry entry)
        {
            return _entryRepository.Update(entry);
        }
        public IEnumerable<T> QueryBySql<T>(string sql)
        {
            return _entryRepository.QueryBySql<T>(sql);
        }
        #endregion

        #region  业务方法

        public Hashtable ListData(FinanceFilter filter)
        {
            int count;
            var baseQuery = @"
            SELECT 
    e.Id,
    e.EntryPrice,
    e.Type,
    e.CreateTime,
    e.Verify,
    c.ContractNumber,       
    par.ParturientName,      
    p.PackageName,           
    ep.Name AS EntryPeopleName,
    v.Name AS VerifyName,    
    f.Name AS FounderName     
FROM 
    Entry AS e
    LEFT JOIN Contract AS c ON c.Id = e.ContractId
    LEFT JOIN Parturient AS par ON par.Id = c.ParturientId
    LEFT JOIN Package AS p ON p.Id = par.PackageId
    LEFT JOIN Staff AS ep ON ep.Id = e.EntryPeopleId
    LEFT JOIN Staff AS v ON v.Id = e.VerifyId
    LEFT JOIN Staff AS f ON f.Id = e.FounderId;";

            var entryRecords = QueryBySql<FinanceVM>(baseQuery).AsQueryable();

            Hashtable hashtable = new Hashtable();
            hashtable["code"] = 0;
            hashtable["message"] = "success";
            hashtable["data"] = MakeQueryFilter(entryRecords, filter, out count);
            hashtable["count"] = count; // 结束惰性加载
            return hashtable;
        }



        #endregion

        #region 公共方法
        public IQueryable<FinanceVM> MakeQueryFilter(IQueryable<FinanceVM> finance, FinanceFilter financeFilter, out int count)
        {
            // 删除状态过滤
            if (financeFilter.IsDelete != null)
            {
                finance = finance.AsEnumerable()
                    .Where(x => x.IsDelete == financeFilter.IsDelete)
                    .AsQueryable();
            }

            // 添加日期范围过滤
            if (!string.IsNullOrEmpty(financeFilter.StartDate))
            {
                DateTime startDate = DateTime.Parse(financeFilter.StartDate);
                finance = finance.AsEnumerable()
                    .Where(x => x.CreateTime >= startDate)
                    .AsQueryable();
            }

            if (!string.IsNullOrEmpty(financeFilter.EndDate))
            {
                DateTime endDate = DateTime.Parse(financeFilter.EndDate).AddDays(1);
                finance = finance.AsEnumerable()
                    .Where(x => x.CreateTime < endDate)
                    .AsQueryable();
            }

            // 添加类型过滤
            if (financeFilter.Type != null)
            {
                finance = finance.AsEnumerable()
                    .Where(x => x.Type == financeFilter.Type)
                    .AsQueryable();
            }

            // 添加审核状态过滤
            if (financeFilter.Verify != null)
            {
                finance = finance.AsEnumerable()
                    .Where(x => x.Verify == financeFilter.Verify)
                    .AsQueryable();
            }

            // 添加合同编号过滤
            if (!string.IsNullOrEmpty(financeFilter.ContractNumber))
            {
                finance = finance.AsEnumerable()
                    .Where(x => x.ContractNumber != null &&
                                 x.ContractNumber.Contains(financeFilter.ContractNumber))
                    .AsQueryable();
            }

            count = finance.Count();

            // 分页处理
            if (financeFilter.page != 0 && financeFilter.limit != 0)
                return finance.OrderBy(x => x.Id).Skip((financeFilter.page - 1) * financeFilter.limit).Take(financeFilter.limit);
            else
                return finance;
        }
        #endregion
    }
}
